package com.troubadorian.feeds.customviews;

import com.troubadorian.feeds.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RepeatingDarkImageView extends ImageView {
	private Context mContext;
	private int viewWidth;
	private Bitmap bgTile;

	public RepeatingDarkImageView(Context context) {
		super(context);
		mContext = context;
		try {
			bgTile = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zigzag_dark);
		} catch (Throwable e) {
			bgTile = null;
		}
	}

	public RepeatingDarkImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		try {
			bgTile = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zigzag_dark);
		} catch (Throwable e) {
			bgTile = null;
		}
	}

	public RepeatingDarkImageView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		try {
			bgTile = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.zigzag_dark);
		} catch (Throwable e) {
			bgTile = null;
		}
	}

	@Override
	protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
		super.onSizeChanged(xNew, yNew, xOld, yOld);

		viewWidth = xNew;
	}

	@Override
	public void onDraw(Canvas canvas) {
		if (bgTile == null)
			return;

		float left = 0;
		float bgTileWidth = bgTile.getWidth();

		while (left < viewWidth) {
			if (left + bgTileWidth <= viewWidth)
				canvas.drawBitmap(bgTile, left, 0, null);
			left += bgTileWidth;
		}
	}
}
